package freespiritsocial.io.user.viewmodel

import androidx.lifecycle.MutableLiveData
import freespiritsocial.io.user.data.model.bean.ApiRefreshResult
import freespiritsocial.io.user.data.model.bean.PostPojo
import freespiritsocial.io.user.network.ApiService.Companion.PAGE_SIZE
import freespiritsocial.io.user.network.apiService
import freespiritsocial.io.user.viewmodel.delegates.IPostApi
import freespiritsocial.io.user.viewmodel.delegates.PostDelegate
import freespiritsocial.io.user.viewmodel.delegates.UserInfoViewModel
import me.hgj.jetpackmvvm.callback.livedata.UnPeekLiveData
import me.hgj.jetpackmvvm.ext.request

/**
 * @description
 * @author csw
 * @date 2021-10-18
 */
class FollowingViewModel: UserInfoViewModel(),IPostApi by PostDelegate() {
    val postResult = MutableLiveData<ApiRefreshResult<PostPojo>>()
    val postItemResult = UnPeekLiveData<PostPojo>()

    var postId: Long? = null
    fun getFollowingPostList(refresh: Boolean = false) {
        request({
            if (refresh) postId = null
            apiService.getFollowingPostList(postId)
        },{
            postId = it.lastOrNull()?.invitationId
            postResult.postValue(ApiRefreshResult(refresh,it,it.size >= PAGE_SIZE))
        })
    }

    fun getPost(id: Long?) {
        request({
            apiService.getInvitationDetail(id)
        },{
            postItemResult.value = it
        },isShowDialog = true)
    }


}